<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
include '../Module/TemplateModule.php';
include '../Module/OrganizationModule.php';
include '../Module/CategoryModule.php';
$caModule=new CategoryModule();
$orModule=new OrganizationModule();
$dtca=$caModule->getCategorys($_type);
$dtor=$orModule->getOrganizationAll($_type);

$itemModule=new TemplateModule();
if(isset($_REQUEST["id"])){
$id= $_REQUEST["id"];
$item=$itemModule->getTemplate($id);
}
?>
<div class="mybutton icon " onclick="hideitemdetail();"><div><img src="../images/icon/left1.png"/><span>Back</span></div></div>
<div class="mybutton icon spaceright" onclick="<?=isset($id)?"loaditemdetail('$id')":"loaditemdetail(null);"?>"><div><img src="../images/icon/arrow_refresh1.png"/><span>Reload</span></div></div>
<div class="mybutton icon <?=isset($item["delete"])?" disable":""?>"><div><img src="../images/icon/media_floppy_green.png"/><span>Save</span></div></div>
<div class="mybutton icon<?=isset($id)?"":" disable"?>" onclick="<?=isset($id)?"loaditemdetail(null);":""?>"><div><img src="../images/icon/addg.png"/><span>Add</span></div></div>

<div style="clear: both"></div>
<div style="height: 40px;width: 100%;text-align: center">
    <h1><?=isset($id)?"Edit Item : ":"New Item"?><?=$item["name"]?></h1>
</div>
<div style="width: 100%">
    <table style="width:100%;" class="_form">
                <tr>
                    <td style="">
                        <div class="_input-label">
                            <div class="input">
                                <input type="text" id="txtnamei" placeholder="" value="<?=$item["name"]?>"/>
                                <input type="hidden" id="txtidi" placeholder="" value="<?=$item["id"]?>"/>
                                <div class="label"><label>Item Name</label></div>
                            </div>
                        </div>
                    </td>
                    <td class="space"></td>
                    <td>
                        <div class="_input-label" type="select">
                        <div class="input" type="select">
                            <div class="select">
                                <select id="cbxcategoryi">
                                    <?
                                    foreach ($dtca as $row){
                                        $lv_cur=count(explode(".", $row["level"]));
                                        $sp="";
                                        if($lv_cur==3)$sp="&nbsp;&nbsp;&nbsp;&nbsp;";
                                        if($lv_cur==4)$sp="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                                        if($lv_cur==5)$sp="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                                        if($row["id"]==$item["category"])
                                            echo "<option selected=1 value='".$row["id"]."'>".$sp.$row["name"]."</option>";
                                        else
                                            echo "<option value='".$row["id"]."'>".$sp.$row["name"]."</option>";


                                    }
                                    ?>
                                </select>
                            </div>
                            <div class="label"><label>Category</label></div>
                        </div>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td style="">
                        <div class="_input-label" type="select">
                        <div class="input" type="select">
                            <div class="select">
                                <select id="cbxorganizationi">
                                    <?
                                    foreach ($dtor as $row){
                                        if($row["id"]==$item["organization"])
                                            echo "<option selected=1 value='".$row["id"]."'>".$row["name"]."</option>";
                                        else
                                            echo "<option value='".$row["id"]."'>".$row["name"]."</option>";


                                    }
                                    ?>
                                </select>
                            </div>
                            <div class="label"><label>Organization</label></div>
                        </div>
                        </div>
                    </td>
                    <td class="space"></td>
                    <td>
                        <div class="_input-label">
                            <div class="input">
                                <input type="text" id="txtprice" placeholder="" value="<?=$item["price"]?>"/>
                                <div class="label"><label>Price</label></div>
                            </div>
                        </div>
                        
                    </td>
                </tr>
                <tr>
                    <td>
                        <div class="_input-label">
                            <div class="input">
                                <input type="text" id="txtamount" placeholder="" value="<?=$item["amount"]?>"/>
                                <div class="label"><label>Amount</label></div>
                            </div>
                        </div>
                    </td>
                    <td class="space"></td>
                    <td>
                        <div class="_input-label">
                            <div class="input">
                                <input type="text" id="" placeholder="" value="<?=$item[""]?>"/>
                                <div class="label"><label></label></div>
                            </div>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>
                        <div class="_input-label">
                            <div class="input">
                                <input type="text" id="txtimagei" placeholder="" value="<?=$item["img"]?>"/>
                                <div class="label"><label>Image</label></div>
                            </div>
                        </div>
                    </td>
                    <td class="space"></td>
                    <td>
                        <div class="_input-label">
                            <div class="input">
                                <input type="text" id="txtimg1" placeholder="" value="<?=$item["img1"]?>"/>
                                <div class="label"><label>Image1</label></div>
                            </div>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>
                        <div class="_input-label">
                            <div class="input">
                                <input type="text" id="txtimg2" placeholder="" value="<?=$item["img2"]?>"/>
                                <div class="label"><label>Image2</label></div>
                            </div>
                        </div>
                    </td>
                    <td class="space"></td>
                    <td>
                        <div class="_input-label">
                            <div class="input">
                                <input type="text" id="txtimg3" placeholder="" value="<?=$item["img3"]?>"/>
                                <div class="label"><label>Image3</label></div>
                            </div>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>
                        <div class="_input-label">
                            <div class="input">
                                <input type="text" id="txtimg4" placeholder="" value="<?=$item["img4"]?>"/>
                                <div class="label"><label>Image4</label></div>
                            </div>
                        </div>
                    </td>
                    <td class="space"></td>
                    <td>
                        <div class="_input-label">
                            <div class="input">
                                <input type="text" id="txtimg5" placeholder="" value="<?=$item["img5"]?>"/>
                                <div class="label"><label>Image5</label></div>
                            </div>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td style="" colspan="3">
                        <div class="_input-label">
                            <div class="input">
                                <input type="text" id="txttitlei" placeholder="" value="<?=$item["title"]?>"/>
                                <div class="label"><label>Title</label></div>
                            </div>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td style="" colspan="3">
                        <div class="_input-label">
                            <div class="input">
                                <input type="text" id="txthomepagei" placeholder="" value="<?=$item["homepage"]?>"/>
                                <div class="label"><label>Home Page</label></div>
                            </div>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td style="" colspan="3">
                        <div class="_input-label">
                            <div class="input">
                                <input type="text" id="txtweb1i" placeholder="" value="<?=$item["website1"]?>"/>
                                <div class="label"><label>Website 1</label></div>
                            </div>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td colspan="3">
                        <div class="input-full" label="area">
                            <div class="label">
                                <label>Brief Note</label>
                            </div>
                            
                            <textarea rows="4" id="txtbrief" placeholder=""><?=$item["brief"]?></textarea>
                        </div>
                        <!--<div class="input-full">
                            <div class="label"><label>Brief Note</label></div>
                            
                            
                        </div>
                        <div style="padding:27px 10px 0 0;">
                            <textarea class="_s _w100" id="html-editor-brief" rows="3" cols="103" style=""><?=$item["brief"]?></textarea>
                        </div>-->
                    </td>
                </tr>
                <tr>
                    <td colspan="3">
                        <div class="input-full" label="area">
                            <div class="label">
                                <label>Description</label>
                            </div>
                            
                            <textarea rows="4" id="txtdes" placeholder=""><?=$item["description"]?></textarea>
                        </div>
                        <!--<div class="input-full">
                            <div class="label"><label>Description</label></div>
                            
                            
                        </div>
                        <div style="padding:27px 10px 0 0;">
                            <textarea class="_s _w100" id="html-editor-des" rows="3" cols="103" style=""><?=$item["description"]?></textarea>
                        </div>-->
                    </td>
                </tr>
                <tr>
                    <td colspan="3" style="">
                        <span style="color: blue">Insert : <?=$item["insert"]?></span>
                        <?
                        $u=$item["update"];$d=$item["delete"];
                        if(isset($item["update"])){
                            echo "<br/><span style='color: orange'>Update : $u</span>";
                        }
                        if(isset($item["delete"])){
                            echo "<br/><span style='color: red'>Delete : $d</span>";
                        }
                        ?>
                        
                    </td>
                </tr>
            </table>
    
    
    <span id="warningmsg"></span>
</div>
<div style="clear: both"></div>

<div class="mybutton icon " onclick="hideitemdetail();"><div><img src="../images/icon/left1.png"/><span>Back</span></div></div>
<div class="mybutton icon spaceright" onclick="<?=isset($id)?"loaditemdetail('$id')":"loaditemdetail(null);"?>"><div><img src="../images/icon/arrow_refresh1.png"/><span>Reload</span></div></div>
<div class="mybutton icon <?=isset($item["delete"])?" disable":""?>"><div><img src="../images/icon/media_floppy_green.png"/><span>Save</span></div></div>
<div class="mybutton icon<?=isset($id)?"":" disable"?>" onclick="<?=isset($id)?"loaditemdetail(null);":""?>"><div><img src="../images/icon/addg.png"/><span>Add</span></div></div>
<div style="clear: both"></div>
<div class="box-menu hover50">
    <div>
    <div class="icon32 hover50" title="Back" onclick="hidebox2();">
        <img src="../images/icon/back.png"/>
    </div>  
    
    <div class="icon32 hover50" title="Save"
         onclick="<?=isset($id)?(isset($item["delete"])?"alert('This item is deleted.');":"update();"):"save()"?>"
         >
        <img src="../images/icon/tick_.png"/>
    </div>
    <div class="icon32 hover50" onclick="<?=isset($id)?"loaditem('$id')":"newitem();"?>" title="Refresh">
        <img class="" src="../images/icon/refresh1.png"/>
    </div>
    <?
    if(isset($id)){
    ?>
    <div class="icon32 hover50" onclick="newitem()" title="Go to New Item">
        <img class="" src="../images/icon/add2.png"/>
    </div>
    <?
    }
    ?>
    </div>
</div>
<script>
    function save(){
        process("add");
    }
    function update(){
        process("edit");
    }
    function process(action){
        if(!_FcheckFilled($("#txtnamei").val())){$("#txtnamei").focus();return;}
        if(!_FcheckFilled($("#txtimagei").val())){$("#txtimagei").focus();return;}
        if(!_FcheckFilled($("#txttitlei").val())){$("#txttitlei").focus();return;}
        if(!_FcheckFilled($("#txtprice").val())){$("#txtprice").focus();return;}
        if(!_FcheckNumber($("#txtprice").val())){$("#txtprice").focus();return;}
        if(!_FcheckFilled($("#txtamount").val())){$("#txtamount").focus();return;}
        if(!_FcheckNumber($("#txtamount").val())){$("#txtamount").focus();return;}
        var surl="../Controller/";
        var sdata={
                "controller":"TemplateController",
                "action":action,
                "_type":"<?=$_type?>",
                "id":$("#txtidi").val(),
                "name":$("#txtnamei").val(),
                "category":$("#cbxcategoryi").val(),
                "organization":$("#cbxorganizationi").val(),
                "image":$("#txtimagei").val(),
                "img1":$("#txtimg1").val(),
                "img2":$("#txtimg2").val(),
                "img3":$("#txtimg3").val(),
                "img4":$("#txtimg4").val(),
                "img5":$("#txtimg5").val(),
                "price":$("#txtprice").val(),
                "amount":$("#txtamount").val(),
                "title":$("#txttitlei").val(),
                "brief":$("#txtbrief").val(),
                "des":$("#txtdes").val(),
                "homepage":$("#txthomepagei").val(),
                "website1":$("#txtweb1i").val()
                
        };
        jQuery.ajax({
                type:"POST", 
                data:sdata, 
                dataType:"text", 
                url:surl, 
                success: function (data){
                    if(data=="true"){
                        $("#warningmsg").html("Sucessful. Form will reset in 3 second.");
                        $("#warningmsg").show();
                        setTimeout(function() {
                            $("#warningmsg").hide();
                        },3000);
                        showmsg("Sucessful.",2000);
                    }else{
                        $("#warningmsg").html(data);
                        $("#warningmsg").show();
                    }

                },
                error: function (xhr, ajaxOptions, thrownError){
                        $("#warningmsg").html("<b>Status</b>:"+xhr.status+"<br/><b>ThrownError</b>:"+thrownError);
                        $("#warningmsg").show();
                }
        });

    }
</script>